// pages/community/community.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classify:'fuli',
    dataList:[],
    // tab列表
    classes:[],
    activeTab:0,
    banners:[],
    toplist:[],
    isDark:'',
    showAddCommunity:false,
    content:'',
    title:'',
    userInfo:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '社区',
    })

    let storageUserInfo = wx.getStorageSync('userInfo') ? wx.getStorageSync('userInfo'):''
    this.setData({
      userInfo:storageUserInfo
    })

    let storageDark = wx.getStorageSync('storageDark') ? wx.getStorageSync('storageDark') : false
    this.setData({
      isDark: storageDark
    })


    this.getData()

    
  },

  getData(){
    this.setData({
      dataList:[]
    })
    wx.request({
      url: `https://app.kujiang.com/v1/community/get_list_201805?page=1&classify=${this.data.classify}`,
      success:({data})=>{
    
        if (this.data.userInfo) {
          let addStorageCommunity = wx.getStorageSync('storageCommunity')?wx.getStorageSync('storageCommunity'):[]
          addStorageCommunity.map(t=>{
            if (t.classify==this.data.classify) {
              data.body.list.unshift(t)
            }
          })
        }
     

        this.setData({
          dataList:data.body
        })

        if (this.data.classes.length==0) {
             this.setData({
               classes:data.body.classes,
               banners:data.body.banners,
               toplist:data.body.toplist
             })
        }
      }
    })
  },

  onChange(e){
    this.setData({
      classify:this.data.classes[e.detail.name].code
    })
    this.getData()
  },

  changeShowAddCommunity(){
    this.setData({
      showAddCommunity:!this.data.showAddCommunity
    })
  },

  addCommunity(){

    if (this.data.userInfo) {
      let addStorageCommunity = wx.getStorageSync('storageCommunity')?wx.getStorageSync('storageCommunity'):[]
    addStorageCommunity.unshift({
      content:this.data.content,
      title:this.data.title,
      time:"04-13",
      v_user:this.data.userInfo.nickName,
      img_url:this.data.userInfo.avatarUrl,
      classify:this.data.classify
    })
    this.data.dataList.list.unshift(addStorageCommunity)
    wx.setStorageSync('storageCommunity', addStorageCommunity)
    this.setData({
      content:'',
      title:'',
      showAddCommunity:false
    })
    }else {
      this.getUserInfo()
    }

    
  },

   // 登录
   getUserInfo() {
    
    let self = this
    wx.getUserProfile({
      desc: "获取你的昵称、头像、地区及性别", // 不写不弹提示框
      success: res => {
        console.log(res)
        self.setData({
          userInfo: res.userInfo
        })
        wx.setStorageSync('userInfo',  res.userInfo)
      },
      fail: res => {
        //拒绝授权
        wx.showToast({
          title: '您拒绝了授权',
          icon: 'none'
        })
        return;
      }
    })
  },
  onChangeTitle(event) {
    // event.detail 为当前输入的值
    console.log(event.detail);
    this.setData({
      title:event.detail
    })
  },

  onChangeContent(e) {
    // event.detail 为当前输入的值
    console.log(e.detail);
    this.setData({
      content:e.detail
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})